




//
//  FilterResultViewController.m
//  nncloud
//
//  Created by macfeel on 2019/5/31.
//  Copyright © 2019 macfai. All rights reserved.
//

#import "FilterResultViewController.h"
#import "equipDetailModel.h"
#import "RelayCell.h"
#import "RelayDetailViewController.h"

@interface FilterResultViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>

@property (nonatomic,strong)UITableView *tableView;
//@property (nonatomic,strong)NHomeHeaderView *headerView;

@property (nonatomic,strong)NSMutableArray *dataArray;

///
//@property (nonatomic,strong)NSMutableArray *detailDataArray;

@property (nonatomic,assign)NSInteger currentPage;

@property (nonatomic,strong)SearchBar *searchBar;

//@property (nonatomic,strong)LineNameView *lineName;

@property (nonatomic,strong)NSMutableArray *dictArray;

///将线和站的信息传递到下个页面的字典
@property (nonatomic,strong)NSMutableDictionary *transDict;


@end

@implementation FilterResultViewController

#define cellIdentifier  @"relay"
#define HeaderHight  290
#define PageSize 20

-(UITableView *)tableView{
    if (!_tableView) {
        
        WeakStament(ws);
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0 , 0, ScreenWidth , ScreenHeight -NavBarHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 112.0f;
        //        _tableView.separatorColor = [UIColor clearColor];
        _tableView.backgroundColor = kBackgroundColor;
        [_tableView registerNib:[UINib nibWithNibName:@"RelayCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [ws refresh];
        }];
        ///
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            [ws loadMoreData];
        }];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.tableFooterView = [UIView new];
        
    }
    return _tableView;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
//    self.searchBar.hidden = false;
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
//    self.searchBar.hidden = YES;
    ///保证每次进来数组都是空的
    [self.dataArray removeAllObjects];
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.dataArray = [NSMutableArray array];
    self.dictArray = [NSMutableArray array];
    self.currentPage = 1;
    [self.navigationItem setItemWithTitle:@"筛选结果" textColor:kNavigationTitleColor fontSize:18 itemType:center];
    [self.view addSubview:self.tableView];
    
//    [self addSearchBar];
    
    [self loadData];
}

#pragma mark - 添加搜索框
- (void)addSearchBar{
    SearchBar *bar = [[[NSBundle mainBundle]loadNibNamed:@"SearchBar" owner:self options:nil]lastObject];
    bar.frame = CGRectMake(50, 7, ScreenWidth-100, 30);
    bar.layer.cornerRadius = 15.f;
    bar.layer.masksToBounds = YES;
    bar.textFd.delegate = self;
    bar.textFd.returnKeyType = UIReturnKeySearch;
    self.searchBar = bar;
    [self.navigationController.navigationBar addSubview:bar];
}
//搜索虚拟键盘响应
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    
    NSLog(@"点击了搜索");
    [self.searchBar.textFd resignFirstResponder];
    //进行搜索
    [self SearchData];
    return YES;
    
}


#pragma mark - 数据请求

-(void)loadData{
    
    if (self.currentPage==1) {
        [self.dataArray removeAllObjects];
    }
    
    ///type 1继电器  2转辙机  3综合器材  brokenNumber 段编号这个参数主要是在搜索时候用
    NSString *url;
    if ([[NUserModel shareInstance].equipType isEqualToString:@"r"]) {
        url = kFilterRelayUrl;
    }else if ([[NUserModel shareInstance].equipType isEqualToString:@"s"]){
        url = kFilterSwitchUrl;
    }else if ([[NUserModel shareInstance].equipType isEqualToString:@"c"]){
        url = kFilterCMUrl;
    }else{
        url = kFilterCamelUrl;
    }
    
    NSDictionary *dict = @{@"data":self.filterJsonString,@"currentPage":@(self.currentPage)};
    [SVProgressHUD show];
    [NNetworkClient postRequestWithBaseUrl:url setParameters:dict success:^(id responseObject) {
        YYLog(@"%@",responseObject);
        if ([responseObject[@"status"]integerValue]==0) {
            
            NSArray *arr = responseObject[@"data"][@"lists"];
            
            for (NSDictionary *dict in arr) {
                //                RelayListModel *model = [RelayListModel mj_objectWithKeyValues:dict];
                equipDetailModel *model = [equipDetailModel mj_objectWithKeyValues:[dict deleteNull]];
                [self.dataArray addObject:model];
            }
            
            //            self.dictArray = (NSMutableArray *)arr;
            [self.tableView reloadData];
            [SVProgressHUD dismiss];
            [self endRefresh];
            ///这个放的位置很重要  加载更多没有数据处理
            if (arr.count==0) {
                [self noMoreData];
            }
        }
        
    } fail:^(NSError *error) {
        YYLog(@"%@",error);
        [SVProgressHUD dismiss];
    } delegater:nil];
}

#pragma mark - 数据搜索
///数据搜索
- (void)SearchData{
    WeakStament(ws);
    if (self.currentPage==1) {
        
        [self.dataArray removeAllObjects];
    }
    
    ///type 1继电器  2转辙机  3综合器材  brokenNumber 段编号这个参数主要是在搜索时候用
    NSString *type;
//    NSString *url;//url 是公用一个 根据传的类型不一样显示的不一样
    if ([[NUserModel shareInstance].equipType isEqualToString:@"r"]) {
        type = @"1";
        
    }else if ([[NUserModel shareInstance].equipType isEqualToString:@"s"]){
        type = @"2";
    }else if ([[NUserModel shareInstance].equipType isEqualToString:@"c"]){
        type = @"3";
    }else{
        type = @"4";
    }
    
    NSDictionary *dict = @{@"type":type,@"brokenNumber":self.searchBar.textFd.text?:@"",@"stationName":[NUserModel shareInstance].currentStation?:@"",@"currentPage":@(self.currentPage)};
    
    [SVProgressHUD show];
    
    [NNetworkClient postRequestWithBaseUrl:kInqueryStationUrl setParameters:dict success:^(id responseObject) {
        
        YYLog(@"%@",responseObject);
        
        if ([responseObject[@"status"]integerValue]==0) {
            
            NSArray *arr = responseObject[@"data"][@"lists"];
            
            for (NSDictionary *dict in arr) {
                //                RelayListModel *model = [RelayListModel mj_objectWithKeyValues:dict];
                equipDetailModel *model = [equipDetailModel mj_objectWithKeyValues:[dict deleteNull]];
                [self.dataArray addObject:model];
            }
            
            //            self.dictArray = (NSMutableArray *)arr;
            [self.tableView reloadData];
            [SVProgressHUD dismiss];
            [ws endRefresh];
            
            ///这个放的位置很重要  加载更多没有数据处理
            
            if (arr.count==0) {
                [ws noMoreData];
            }
        }
    } fail:^(NSError *error) {
        YYLog(@"%@",error);
        [SVProgressHUD dismiss];
    } delegater:nil];
}
- (void)refresh {
    self.currentPage = 1;
    [self loadData];
}
- (void)loadMoreData{
    
    self.currentPage++;
    [self loadData];
    
    
}
- (void)endRefresh{
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
}

- (void)noMoreData {
    [self.tableView reloadData];
    [self.tableView.mj_footer endRefreshingWithNoMoreData];
}
#pragma mark - datasource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
    //    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    RelayCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    //    RelayListModel *model = self.dataArray[indexPath.row];
    equipDetailModel *model;
//    equipDetailModel *model;
    if (self.dataArray.count>indexPath.row) {
        model = self.dataArray[indexPath.row];
    }
    
    if ([[NUserModel shareInstance].equipType isEqualToString:@"r"]) {
        
        cell.bianhaoLab.text = model.duanbianhao;
        cell.xinghaoLab.text = model.xinghao;
        cell.stationLab.text = model.zhanming;
        cell.positionLab.text = model.zuheweizhi;
        
    }else if ([[NUserModel shareInstance].equipType isEqualToString:@"s"]){
        
        cell.zhanmingOrQuxiangLab.text = @"去向";
        cell.weizhiOrChahaoLab.text = @"岔号";
        cell.bianhaoLab.text = model.shebeibianhao;
        cell.xinghaoLab.text = model.shebeixinghao;
        cell.stationLab.text = model.quxiang;
        cell.positionLab.text = model.chahao;
        
    }else if ([[NUserModel shareInstance].equipType isEqualToString:@"c"]){
        
        cell.bianhaoLab.text = model.duanbianhao;
        cell.xinghaoLab.text = model.xinghao;
        cell.stationLab.text = model.zhanming;
        cell.positionLab.text = model.usePosition;
    }else{
        //驼峰
        cell.zhanmingOrQuxiangLab.text = @"去向";
        cell.weizhiOrChahaoLab.text = @"岔号";
        cell.bianhaoLab.text = model.shebeibianhao;
        cell.xinghaoLab.text = model.shebeixinghao;
        cell.stationLab.text = model.quxiang;
        cell.positionLab.text = model.chahao;
    }
    
    return cell;
}


#pragma mark - delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    equipDetailModel *model = self.dataArray[indexPath.row];
    RelayDetailViewController *proc = [RelayDetailViewController new];
    proc.flag = @"r";
    proc.model = model;
    [self.navigationController pushViewController:proc animated:YES];
}

@end
